package org.urbansoft.icl.record;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import org.urbansoft.icl.field.Field;
import org.urbansoft.icl.field.GenericANField;
import org.urbansoft.icl.field.GenericBlankField;
import org.urbansoft.icl.field.GenericNBField;
import org.urbansoft.icl.field.GenericNField;
import org.urbansoft.icl.field.Position;
import org.urbansoft.icl.field.Usage;
import org.urbansoft.icl.field.ValidationCriteria;
import org.urbansoft.icl.field.validation.CollectionTypeIndicatorValidator;
import org.urbansoft.icl.field.validation.RecordTypeValidator;
import org.urbansoft.icl.field.validation.RoutingNumberValidator;
import org.urbansoft.icl.util.EasternStandardTime;
import org.urbansoft.icl.util.RightJustifySpacePadder;

/**
 * The Class BundleHeaderRecord.
 */
public class BundleHeaderRecord extends Record {
	private static Logger log = Logger.getLogger(BundleHeaderRecord.class);
	public Field recordType;
	public Field collectionTypeIndicator;
	public Field destinationRoutingNumber;
	public Field eceInstitutionRoutingNumber;
	public Field bundleBusinessDate;
	public Field bundleCreationDate;
	public Field bundleID;
	public Field bundleSequenceNumber;
	public Field cycleNumber;
	public Field returnLocationRoutingNumber;
	public Field userField;
	public Field reserved;
	public Field reservedUF;

	/**
	 * Instantiates a new bundle header record.
	 */
	public BundleHeaderRecord() {
		fields = new ArrayList<Field>();
		recordName = "Bundle Header Record";
		recordTypeNumber = "20";
		initFields();
	}

	/**
	 * Instantiates a new bundle header record for read purpose
	 */
	public BundleHeaderRecord(String read) {
		// log.info("New BundleHeaderRecord for READ");
		fields = new ArrayList<Field>();
		recordName = "Bundle Header Record";
		recordTypeNumber = "20";
		initFields2();
	}

	/**
	 * Instantiates a new bundle header record for write purpose
	 */
	public BundleHeaderRecord(int read) {
		// log.info("New BundleHeaderRecord for WRITE");
		fields = new ArrayList<Field>();
		recordName = "Bundle Header Record";
		recordTypeNumber = "20";
		initFields3();
	}

	/**
	 * Inits the fields.
	 */
	private void initFields2() {
		try {
			/* bundle header record */
			recordType = new GenericANField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "20", "Record Type");

			/* Collection Type Indicator */
			collectionTypeIndicator = new GenericANField(new Position(3, 4),
					"Collection Type Indicator");

			/* routing numbers */
			destinationRoutingNumber = new GenericANField(new Position(5, 13),
					"Destination Routing Number");

			eceInstitutionRoutingNumber = new GenericANField(new Position(14,
					22), "ECE Institution Routing Number");

			/* dates - default to now */
			bundleBusinessDate = new GenericANField(new Position(23, 30),
					"Bundle Business Date");

			bundleCreationDate = new GenericANField(new Position(31, 38),
					"Bundle Creation Date");

			/* bundle ID */
			bundleID = new GenericANField(new Position(39, 48), "Bundle ID");

			/* bundle sequence number */
			bundleSequenceNumber = new GenericANField(new Position(49, 52),
					"Bundle Sequence Number");

			/* cycle number */
			cycleNumber = new GenericANField(new Position(53, 54),
					"Cycle Number");

			/* return location routing number */
			returnLocationRoutingNumber = new GenericANField(new Position(55,
					63), "Return Location Routing Number");

			/* user field - NOT USED */
			userField = new GenericANField(new Position(64, 68), "User Field");

			/* Reserved - NOT USED */
			reserved = new GenericANField(new Position(69, 80), "Reserved");

			/* Reserved - NOT USED */
			reservedUF = new GenericANField(new Position(81, 90),
					"Reserved User Field");

			fields.add(recordType);
			fields.add(collectionTypeIndicator);
			fields.add(destinationRoutingNumber);
			fields.add(eceInstitutionRoutingNumber);
			fields.add(bundleBusinessDate);
			fields.add(bundleCreationDate);
			fields.add(bundleID);
			fields.add(bundleSequenceNumber);
			fields.add(cycleNumber);
			fields.add(returnLocationRoutingNumber);
			fields.add(userField);
			fields.add(reserved);
			fields.add(reservedUF);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields3() {
		try {
			/* bundle header record */
			recordType = new GenericANField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "20", "Record Type");

			/* Collection Type Indicator */
			collectionTypeIndicator = new GenericANField(new Position(3, 4),
					"Collection Type Indicator");
			collectionTypeIndicator.setFieldData("");

			/* routing numbers */
			destinationRoutingNumber = new GenericANField(new Position(5, 13),
					"Destination Routing Number");
			destinationRoutingNumber.setFieldData("");

			eceInstitutionRoutingNumber = new GenericANField(new Position(14,
					22), "ECE Institution Routing Number");
			eceInstitutionRoutingNumber.setFieldData("");

			/* dates - default to now */
			bundleBusinessDate = new GenericANField(new Position(23, 30),
					"Bundle Business Date");
			bundleBusinessDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			bundleCreationDate = new GenericANField(new Position(31, 38),
					"Bundle Creation Date");
			bundleCreationDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			/* bundle ID */
			bundleID = new GenericANField(new Position(39, 48), "Bundle ID");
			bundleID.setFieldData("");

			/* bundle sequence number */
			bundleSequenceNumber = new GenericANField(new Position(49, 52),
					"Bundle Sequence Number");
			bundleSequenceNumber.setFieldData("");

			/* cycle number */
			cycleNumber = new GenericANField(new Position(53, 54),
					"Cycle Number");
			cycleNumber.setFieldData("");

			/* return location routing number */
			returnLocationRoutingNumber = new GenericANField(new Position(55,
					63), "Return Location Routing Number");
			returnLocationRoutingNumber.setFieldData("");

			/* user field - NOT USED */
			userField = new GenericANField(new Position(64, 68), "User Field");
			userField.setFieldData("");

			/* Reserved - NOT USED */
			reserved = new GenericANField(new Position(69, 80), "Reserved");
			reserved.setFieldData("");

			/* Reserved - NOT USED */
			reservedUF = new GenericANField(new Position(81, 90),
					"Reserved User Field");
			reservedUF.setFieldData("");

			fields.add(recordType);
			fields.add(collectionTypeIndicator);
			fields.add(destinationRoutingNumber);
			fields.add(eceInstitutionRoutingNumber);
			fields.add(bundleBusinessDate);
			fields.add(bundleCreationDate);
			fields.add(bundleID);
			fields.add(bundleSequenceNumber);
			fields.add(cycleNumber);
			fields.add(returnLocationRoutingNumber);
			fields.add(userField);
			fields.add(reserved);
			fields.add(reservedUF);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields() {
		try {
			/* bundle header record */
			recordType = new GenericNField(new Position(1, 2), "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* Collection Type Indicator */
			collectionTypeIndicator = new GenericNField(new Position(3, 4),
					"Collection Type Indicator");
			collectionTypeIndicator
					.setFieldValidator(CollectionTypeIndicatorValidator.INSTANCE);
			collectionTypeIndicator
					.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* routing numbers */
			destinationRoutingNumber = new GenericNField(new Position(5, 13),
					"Destination Routing Number");
			destinationRoutingNumber
					.setFieldValidator(RoutingNumberValidator.INSTANCE);
			destinationRoutingNumber
					.setValidationCriteria(ValidationCriteria.REQUIRED_IF_PRESENT);

			eceInstitutionRoutingNumber = new GenericNField(
					new Position(14, 22), "ECE Institution Routing Number");
			eceInstitutionRoutingNumber
					.setFieldValidator(RoutingNumberValidator.INSTANCE);
			eceInstitutionRoutingNumber
					.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* dates - default to now */
			bundleBusinessDate = new GenericNField(new Position(23, 30),
					"Bundle Business Date");
			bundleBusinessDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			bundleCreationDate = new GenericNField(new Position(31, 38),
					"Bundle Creation Date");
			bundleCreationDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			/* bundle ID */
			bundleID = new GenericANField(new Position(39, 48), "Bundle ID");
			bundleID.setPadder(RightJustifySpacePadder.SPACE_PADDER);
			bundleID.setUsage(Usage.CONDITIONAL);
			bundleID.setValidationCriteria(ValidationCriteria.REQUIRED_IF_PRESENT);
			bundleID.setFieldData("");

			/* bundle sequence number */
			bundleSequenceNumber = new GenericNBField(new Position(49, 52),
					"Bundle Sequence Number");
			bundleSequenceNumber.setUsage(Usage.CONDITIONAL);

			/* cycle number */
			cycleNumber = new GenericANField(new Position(53, 54),
					"Cycle Number");
			cycleNumber.setUsage(Usage.CONDITIONAL);

			/* return location routing number */
			returnLocationRoutingNumber = new GenericNField(
					new Position(55, 63), "Return Location Routing Number");
			returnLocationRoutingNumber.setUsage(Usage.CONDITIONAL);
			returnLocationRoutingNumber.setFieldData("");

			/* user field - NOT USED */
			userField = new GenericANField(new Position(64, 68), "User Field");
			userField.setUsage(Usage.CONDITIONAL);
			userField.setFieldData("");

			/* Reserved - NOT USED */
			reserved = new GenericBlankField(new Position(69, 80),
					"Reserved Field");
			reserved.setUsage(Usage.CONDITIONAL);
			reserved.setFieldData("");

			fields.add(recordType);
			fields.add(collectionTypeIndicator);
			fields.add(destinationRoutingNumber);
			fields.add(eceInstitutionRoutingNumber);
			fields.add(bundleBusinessDate);
			fields.add(bundleCreationDate);
			fields.add(bundleID);
			fields.add(bundleSequenceNumber);
			fields.add(cycleNumber);
			fields.add(returnLocationRoutingNumber);
			fields.add(userField);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

}
